package com.team.app.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.team.app.model.Vehicle;
import com.team.app.model.VehicleView;

@Repository
public class VehicleDaoImpl extends AbstractDao<Vehicle> implements VehicleDao {
	VehicleDaoImpl(){
		super(Vehicle.class);	
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Vehicle> findVehicle( Long modelId) {
		Session s=this.getCurrentSession();
		Query q = s.createQuery("from Vehicle where model_id='"+modelId+"'");
	/*	q.setParameter(0, modelId);*/
		//q.setParameter(n, neu co);
		List<Vehicle> result = q.list(); 			
		
		return result;		
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<VehicleView> findAllView() {
		Session s=this.getCurrentSession();
		Query q = s.createQuery("from VehicleView order by count_feedback desc");
		
		List<VehicleView> result = q.setMaxResults(10).list(); 			
		
		return result;	
	}	
	
	@Override
	@SuppressWarnings("unchecked")
	public List<VehicleView> findTopCar() {
		Session s = this.getCurrentSession();
		Query q = s.createQuery("from VehicleView order by count_feedback desc");
		
		List<VehicleView> result = q.setMaxResults(10).list(); 			
		
		return result;	
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<VehicleView> findCar(Date pickupDate, Date returnDate, Long cityID, String numberSeat, Long brandID, Long transmissionType ){
		Session s = this.getCurrentSession();
		
		System.out.print("Transmission la: " +  transmissionType);
		Long fromSeat = Long.parseLong(numberSeat.split("-")[0].trim());
		Long toSeat = Long.parseLong(numberSeat.split("-")[1].trim());
		Query query = s.createSQLQuery(
				"CALL FindCar(:_pickup_date, :_return_date, :_city_id, :_from_number_of_seat, :_to_number_of_seat, :_brand_id, :_transmission)")
				.addEntity(VehicleView.class)
				.setParameter("_pickup_date", pickupDate)
				.setParameter("_return_date", returnDate)
				.setParameter("_city_id", cityID)
				.setParameter("_from_number_of_seat", fromSeat)
				.setParameter("_to_number_of_seat", toSeat)
				.setParameter("_brand_id", brandID)
				.setParameter("_transmission", transmissionType);
			 
		List<VehicleView> result = query.list();
		return result;	
	}
	
}
